package com.yys.apm.config;

import com.alibaba.fastjson.JSONObject;
import com.yys.apm.common.annotion.IgnoreFormatReturn;
import com.yys.apm.common.enums.ErrorType;
import com.yys.apm.common.response.Response;
import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;


/**
 * @author Yingsheng Ye
 */
@ControllerAdvice
public class ResponseBodyAdviceConfig implements ResponseBodyAdvice<Object> {

    /**
     * 这个方法表示对于哪些请求要执行beforeBodyWrite，返回true执行，返回false不执行
     */
    @Override
    public boolean supports(MethodParameter methodParameter, Class<? extends HttpMessageConverter<?>> aClass) {
        RestController restController = methodParameter.getContainingClass().getAnnotation(RestController.class);
        ResponseBody responseBody = methodParameter.getMethodAnnotation(ResponseBody.class);
        if (null == restController && null == responseBody) {
            return false;
        }
        boolean flag = false;
        //获取注解
        IgnoreFormatReturn ignoreFormatReturn = methodParameter.getContainingClass().getAnnotation(IgnoreFormatReturn.class);
        if (null == ignoreFormatReturn) {
            flag = true;
            IgnoreFormatReturn ignoreMethodFormatReturn = methodParameter.getMethodAnnotation(IgnoreFormatReturn.class);
            if (null != ignoreMethodFormatReturn) {
                flag = false;
            }
        }
        return flag;

    }

    @Override
    public Object beforeBodyWrite(Object body, MethodParameter methodParameter, MediaType mediaType, Class<? extends HttpMessageConverter<?>> aClass, ServerHttpRequest serverHttpRequest, ServerHttpResponse serverHttpResponse) {
        //验证方面的特殊处理
        if (body instanceof String) {
            return isJson((String) body) ? JSONObject.toJSONString(new Response<>(body, ErrorType.SUCCESS)) : body;
        } else if (body instanceof Response<?>) { //如果已经被异常捕获,返回的就是BaseResponse对象,不用再次封装了
            return body;
        }
        return new Response<>(body, ErrorType.SUCCESS);
    }

    private boolean isJson(String content) {
        if (StringUtils.isEmpty(content)) {
            return false;
        }
        boolean isJsonObject = true;
        boolean isJsonArray = true;
        try {
            JSONObject.parseObject(content);
        } catch (Exception e) {
            isJsonObject = false;
        }
        try {
            JSONObject.parseArray(content);
        } catch (Exception e) {
            isJsonArray = false;
        }
        //不是json格式
        return isJsonObject || isJsonArray;
    }

}
